package cn.wuxing.mediation.businessplatform.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.wuxing.mediation.businessplatform.domain.annotation.BusinessTag;
import cn.wuxing.mediation.businessplatform.domain.constants.BusinessListConstants;
import cn.wuxing.mediation.businessplatform.domain.dto.DelDTO;
import cn.wuxing.mediation.businessplatform.domain.dto.OrdinaryDisputeEventOperateDTO;
import cn.wuxing.mediation.businessplatform.domain.enums.EventCategoryEnum;
import cn.wuxing.mediation.businessplatform.domain.pojo.EventOperateVO;
import cn.wuxing.mediation.businessplatform.domain.vo.OrdinaryDisputeEventDetailVO;
import cn.wuxing.mediation.businessplatform.mapper.business.AllCaseDataMapper;
import cn.wuxing.mediation.businessplatform.mapper.business.CmMediationRecordMapper;
import cn.wuxing.mediation.businessplatform.mapper.business.CmOrdinaryDisputeEventMapper;
import cn.wuxing.mediation.common.core.domain.entity.*;
import cn.wuxing.mediation.common.core.redis.RedisCache;
import cn.wuxing.mediation.common.exception.ServiceException;
import cn.wuxing.mediation.common.utils.SecurityUtils;
import cn.wuxing.mediation.common.utils.des.DESUtil;
import cn.wuxing.mediation.system.domain.SysDisputeType;
import cn.wuxing.mediation.system.mapper.SysDisputeTypeMapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.time.ZoneId;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @description: <描述>
 * @date: 2024/9/15 21:07
 * @author: yang
 * @version: 1.0
 */
@Service
@Transactional(rollbackFor = RuntimeException.class)
@BusinessTag(tag = BusinessListConstants.ORDINARY_DISPUTE_EVENT)
public class OrdinaryDisputeEventOperateServiceImpl extends
        BaseBusinessOperateServiceImpl<OrdinaryDisputeEventOperateDTO, OrdinaryDisputeEventDetailVO> {

    @Autowired
    private CmOrdinaryDisputeEventMapper cmOrdinaryDisputeEventMapper;

    @Autowired
    private CmMediationRecordMapper cmMediationRecordMapper;

    @Autowired
    private SysDisputeTypeMapper sysDisputeTypeMapper;

    @Autowired
    private AllCaseDataMapper allCaseDataMapper;

    @Autowired
    private RedisCache redisCache;

    private static final String PREFIX_CODE = "JF";

    @Override
    Boolean delEvent(DelDTO dto) {
        CmOrdinaryDisputeEvent cmOrdinaryDisputeEvent = new CmOrdinaryDisputeEvent();
        LambdaUpdateWrapper<CmOrdinaryDisputeEvent> wrapper = new LambdaUpdateWrapper<>(cmOrdinaryDisputeEvent);
        wrapper.in(CmOrdinaryDisputeEvent::getId, dto.getIds());
        wrapper.set(CmOrdinaryDisputeEvent::getDelFlag, "2");
        wrapper.set(CmOrdinaryDisputeEvent::getDelTime, DateTime.now());
        AllCaseData allCaseData = new AllCaseData();
        allCaseData.setDeleted(2);
        LambdaUpdateWrapper<AllCaseData> allCaseDataLambdaUpdateWrapper = Wrappers.lambdaUpdate();
        allCaseDataLambdaUpdateWrapper.in(AllCaseData::getBusinessId, dto.getIds());
        allCaseDataLambdaUpdateWrapper.eq(AllCaseData::getEventCategory, BusinessListConstants.ORDINARY_DISPUTE_EVENT);
        allCaseDataMapper.update(allCaseData, allCaseDataLambdaUpdateWrapper);
        return this.cmOrdinaryDisputeEventMapper.update(cmOrdinaryDisputeEvent, wrapper) > 0;
    }

    @Override
    EventOperateVO saveEvent(OrdinaryDisputeEventOperateDTO dto) {
        EventOperateVO vo = new EventOperateVO();
        CmOrdinaryDisputeEvent cmOrdinaryDisputeEvent = BeanUtil.copyProperties(dto,
                CmOrdinaryDisputeEvent.class);

        if (null == cmOrdinaryDisputeEvent.getId()) {
            cmOrdinaryDisputeEvent.setEventCode(this.redisCache.getCode(PREFIX_CODE));
            cmOrdinaryDisputeEvent.setCreateBy(SecurityUtils.getNickName());
            cmOrdinaryDisputeEvent.setCreateTime(DateTime.now());
            cmOrdinaryDisputeEvent.setUpdateTime(DateTime.now());
            cmOrdinaryDisputeEvent.setRegisterUserId(SecurityUtils.getUserId());
            this.cmOrdinaryDisputeEventMapper.insert(cmOrdinaryDisputeEvent);
            AllCaseData allCaseData = getAllCaseData(dto, cmOrdinaryDisputeEvent);
            allCaseDataMapper.insert(allCaseData);
        } else {
            cmOrdinaryDisputeEvent.setUpdateBy(SecurityUtils.getNickName());
            cmOrdinaryDisputeEvent.setUpdateTime(DateTime.now());
            LambdaUpdateWrapper<CmOrdinaryDisputeEvent> updateCmOrdinaryDisputeEventWrapper = new LambdaUpdateWrapper<>();
            updateCmOrdinaryDisputeEventWrapper.eq(CmOrdinaryDisputeEvent::getId, dto.getId());
            if ("4".equals(dto.getOperateType())) {
                updateCmOrdinaryDisputeEventWrapper.set(CmOrdinaryDisputeEvent::getWindowId, null);
            }
            this.cmOrdinaryDisputeEventMapper.update(cmOrdinaryDisputeEvent, updateCmOrdinaryDisputeEventWrapper);
           /* cmOrdinaryDisputeEvent = this.cmOrdinaryDisputeEventMapper.selectById(dto.getId());
            AllCaseData allCaseData = getAllCaseData(dto, cmOrdinaryDisputeEvent);
            LambdaUpdateWrapper<AllCaseData> updateAllCaseDataWrapper = new LambdaUpdateWrapper<>();
            updateAllCaseDataWrapper.eq(AllCaseData::getBusinessId, cmOrdinaryDisputeEvent.getId());
            updateAllCaseDataWrapper.eq(AllCaseData::getEventCategory, BusinessListConstants.ORDINARY_DISPUTE_EVENT);
            allCaseDataMapper.update(allCaseData, updateAllCaseDataWrapper);*/
        }
        dto.setEventCategory(EventCategoryEnum.GENERAL_DISPUTE_EVENT.getCode());
        CmOrdinaryDisputeEvent ordinaryDisputeEvent = cmOrdinaryDisputeEventMapper.selectById(
                cmOrdinaryDisputeEvent.getId());
        if (null == ordinaryDisputeEvent) {
            throw new ServiceException("未找到事件！");
        }
        vo.setId(ordinaryDisputeEvent.getId());
        vo.setEventCode(ordinaryDisputeEvent.getEventCode());
        vo.setEventState(ordinaryDisputeEvent.getEventState());
        return vo;
    }

    private AllCaseData getAllCaseData(OrdinaryDisputeEventOperateDTO dto, CmOrdinaryDisputeEvent entity) {
        AllCaseData allCaseData = new AllCaseData();
        allCaseData.setEventCode(entity.getEventCode());
        SysDisputeType sysDisputeType = this.sysDisputeTypeMapper.selectDisputeTypeByDisputeId(
                Long.parseLong(String.valueOf(entity.getEventType())));

        if (null != sysDisputeType) {
            allCaseData.setEventType(sysDisputeType.getDisputeName());
        }
        allCaseData.setEventDesc(entity.getEventDesc());
        allCaseData.setDsr(dto.getApplicantDTOS()==null||dto.getApplicantDTOS().isEmpty()?"":dto.getApplicantDTOS().stream().map(
                applicantDTO -> DESUtil.encrypt(applicantDTO.getName())).collect(Collectors.joining(",")));
        allCaseData.setAreaId(String.valueOf(dto.getAreaId()));
        allCaseData.setInitiateTime(entity.getInitiateTime());
        allCaseData.setSource("智能矛调应用-一般");
        allCaseData.setBusinessId(String.valueOf(entity.getId()));
        allCaseData.setEventCategory("2");
        allCaseData.setBusinessTypeName(EventCategoryEnum.GENERAL_DISPUTE_EVENT.getDesc());
        return allCaseData;
    }

    @Override
    OrdinaryDisputeEventDetailVO getEventInfoDetail(Long eventId) {
        CmOrdinaryDisputeEvent cmOrdinaryDisputeEvent = this.cmOrdinaryDisputeEventMapper.selectById(eventId);

        if (null == cmOrdinaryDisputeEvent) {
            return null;
        }
        OrdinaryDisputeEventDetailVO ordinaryDisputeEventDetailVO = BeanUtil.copyProperties(cmOrdinaryDisputeEvent,
                OrdinaryDisputeEventDetailVO.class);
        ordinaryDisputeEventDetailVO.setEventCategory(EventCategoryEnum.GENERAL_DISPUTE_EVENT.getCode());
        if (null != cmOrdinaryDisputeEvent.getEventType()) {
            SysDisputeType sysDisputeType = this.sysDisputeTypeMapper.selectDisputeTypeByDisputeId(
                    Long.parseLong(String.valueOf(cmOrdinaryDisputeEvent.getEventType())));

            if (null != sysDisputeType) {
                ordinaryDisputeEventDetailVO.setEventTypeLabel(sysDisputeType.getDisputeName());
            }
        }
        ordinaryDisputeEventDetailVO.setEventType(null != cmOrdinaryDisputeEvent.getEventType() ? String.valueOf(cmOrdinaryDisputeEvent.getEventType()) : null);
        ordinaryDisputeEventDetailVO.setEventSource(null != cmOrdinaryDisputeEvent.getEventSource() ? String.valueOf(cmOrdinaryDisputeEvent.getEventSource()) : null);
        ordinaryDisputeEventDetailVO.setIsBypassLevels(null != cmOrdinaryDisputeEvent.getIsBypassLevels() ? String.valueOf(cmOrdinaryDisputeEvent.getIsBypassLevels()) : null);
        ordinaryDisputeEventDetailVO.setMediateSpecialAttributes(null != cmOrdinaryDisputeEvent.getMediateSpecialAttributes() ? String.valueOf(cmOrdinaryDisputeEvent.getMediateSpecialAttributes()) : null);
        ordinaryDisputeEventDetailVO.setIsConcernSpecialGroups(null != cmOrdinaryDisputeEvent.getIsConcernSpecialGroups() ? String.valueOf(cmOrdinaryDisputeEvent.getIsConcernSpecialGroups()) : null);
        ordinaryDisputeEventDetailVO.setDifficultyLevel(null != cmOrdinaryDisputeEvent.getDifficultyLevel() ? String.valueOf(cmOrdinaryDisputeEvent.getDifficultyLevel()) : null);
        ordinaryDisputeEventDetailVO.setIsDeath(null != cmOrdinaryDisputeEvent.getIsDeath() ? String.valueOf(cmOrdinaryDisputeEvent.getIsDeath()) : null);
        ordinaryDisputeEventDetailVO.setEventPredict(null != cmOrdinaryDisputeEvent.getEventPredict() ? String.valueOf(cmOrdinaryDisputeEvent.getEventPredict()) : null);
        ordinaryDisputeEventDetailVO.setPeopleNum(null != cmOrdinaryDisputeEvent.getPeopleNum() ? String.valueOf(cmOrdinaryDisputeEvent.getPeopleNum()) : null);
        ordinaryDisputeEventDetailVO.setRegisterUser(cmOrdinaryDisputeEvent.getCreateBy());
        ordinaryDisputeEventDetailVO.setRegisterTime(cmOrdinaryDisputeEvent.getCreateTime());

        return ordinaryDisputeEventDetailVO;

    }

    @Override
    public void checkCloseCase(Long eventId) {
        OrdinaryDisputeEventDetailVO eventDetail = (OrdinaryDisputeEventDetailVO) super.getEventDetail(eventId);
        super.checkApplicant(eventDetail.getApplicantDetailVOList());
        if (null == eventDetail.getMediationRecordDetailVO()) {
            throw new ServiceException("请先填写调解记录!");
        }
        if (!new Integer(1).equals(eventDetail.getIsSceneMediation())) {
            super.checkDossier(eventDetail.getDossierFilesVO(),
                    eventDetail.getMediationRecordDetailVO().get(0).getMediationResult());
        }
    }
}
